package com.lib.net

import com.lib.base.data.BaseResult
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author
 * @desc 统一请求处理方式
 */

object ObservableService {

    fun <T> setObservable(observable: Observable<BaseResult<T>>): Observable<T> {
        return observable.map(ApiResponseFunc())
            .onErrorResumeNext(HttpErrorFunc<T>())
            .subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    fun <T> setFlowableBaseResult(observable: Flowable<BaseResult<T>>): Flowable<T> {
        return observable.map(ApiResponseFunc())
            .onErrorResumeNext(HttpErrorFunctionFlowable<T>())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }
}